package com.wxtech.heartbeat.util;

import java.math.BigInteger;

public class IntegerUtil {
	
	//把一个8个字节的二进制转成一个Unsigned整型字符串
	public static String parseU64(byte[] bytes) {
		byte[] reserve = new byte[bytes.length];
		for(int i=0; i< bytes.length; i++) {
			reserve[reserve.length - 1 -i] = bytes[i];
		}
		bytes = reserve;
		BigInteger signed = new BigInteger(bytes);
		return unsigned(signed).toString();
	}

	//把一个2个字节的二进制转成一个Unsigned整型字符串
		public static int parseU16(byte[] bytes) {
			int res = 0;
			res |= (bytes[1] & 0x000000FF) << 8;
			res |= bytes[0] & 0x000000FF;
			return res;
		}
		
	//把Signed大整型数转成Unsigned整型数
	public static BigInteger unsigned(BigInteger b) {
		if (b.signum() >= 0)
			return b;
		byte[] a1 = b.toByteArray();
		byte[] a2 = new byte[a1.length + 1];
		a2[0] = 0;
		System.arraycopy(a1, 0, a2, 1, a1.length);
		return new BigInteger(a2);
	}

}
